package stardust.product.j2d;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferStrategy;
import stardust.product.j2d.buffer.GraphicsBuffer;


public class GraphicsContainer extends Canvas
{
	
	private static final long serialVersionUID = 1L;
	
	public static final int BUFFER_STRATEGY_SIZE = 2;
	public static final int BUFFER_GRAPHICS_SIZE = 16;
	
	private GraphicsBuffer graphicsBuffer;
	private int width;
	private int height;
	
	private GraphicsObject rootGraphicsObject;
	
	public GraphicsContainer()
	{
		
	}

	public GraphicsContainer(int width, int height)
	{
		this.width = width;
		this.height = height;
	}
	
	public void initialize()
	{
		if (this.width == 0)
		{
			this.width = this.getWidth();
		}
		if (this.height == 0)
		{
			this.height = this.getHeight();
		}
		
		// 创建设备双缓冲。
		this.createBufferStrategy(GraphicsContainer.BUFFER_STRATEGY_SIZE);
	
		// 创建绘图缓冲区。
		this.graphicsBuffer = new GraphicsBuffer(GraphicsContainer.BUFFER_GRAPHICS_SIZE, this.width, this.height);
		this.graphicsBuffer.initialize();
	}
	
	public void refresh()
	{
		this.paint();
		this.display();
	}
	
	/**
	 * 执行图形重绘。
	 */
	protected void paint()
	{
		Graphics g = this.graphicsBuffer.getPaintGraphics();

        if (g == null)
        {
            return;
        }

        this.rootGraphicsObject.privatePaint(g, 0, 0);
	}

	/**
	 * 从绘图缓冲中获取已经绘制完成的图像显示在屏幕上。
	 */
	protected void display()
	{
        BufferStrategy bufferStrategy = this.getBufferStrategy();
        Graphics g = bufferStrategy.getDrawGraphics();
        Image displayImage = this.graphicsBuffer.getImage();

        if (displayImage != null)
        {
            g.drawImage(displayImage, 0, 0, null);
        }

        if (displayImage != null)
        {
            String debugString = 
            	"进度(显示/绘制):" + this.graphicsBuffer.getImageIndexCount() + "/" + this.graphicsBuffer.getGraphicsIndexCount() +
            	"    " + 
            	"缓冲区数量(使用/总计):" + (this.graphicsBuffer.getGraphicsIndexCount() - this.graphicsBuffer.getImageIndexCount() - 1) + "/" + this.graphicsBuffer.getBufferSize();
            Font font = new Font(null, 0, 9);

            g.setColor(Color.black);
            g.fillRect(0, 0, this.getWidth(), 10);
            g.setColor(Color.white);
            g.setFont(font);
            g.drawString(debugString.toString(), 0, font.getSize() - 1);
            
            bufferStrategy.show();
        }
	}
	
	public GraphicsObject getRootGraphicsObject()
	{
		return this.rootGraphicsObject;
	}

	public void setRootGraphicsObject(GraphicsObject rootGraphicsObject)
	{
		this.rootGraphicsObject = rootGraphicsObject;
	}
	
	public static GraphicsContainer getSimpleGraphicsContainer(int width, int height)
	{
		GraphicsContainer gc = new GraphicsContainer(width, height);
		
		gc.setRootGraphicsObject(new RootGraphicsObject(width, height));
		
		return gc;
	}
	
}
